package org.zty.service;

import java.util.ArrayList;
import java.util.List;

import org.zty.domain.Attr;
import org.zty.domain.Pojo;

public class ToHtml {
	private final static String listtableClass = "list_table";
	private final static String formtableClass = "form_table";
	public String toList(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		
		sb.append("<table class=\""+listtableClass+"\" cellspacing=0 cellpadding=5 border=0 style=\"width:100%;\">\r\n");
		sb.append("\t<thead style=\"text-align:left;\">\r\n");
		sb.append("\t\t<tr>\r\n");
		for(int i = 0;i<attrList.size();i++){
			Attr attr = attrList.get(i);
			String classstr = "td2";
			if(i%2==0){
				classstr = "td1";
			}
			sb.append("\t\t\t<td class=\""+classstr+"\" align=\"center\">"+attr.getName()+"</td>\r\n");
		}
		sb.append("\t\t</tr>\r\n");
		sb.append("\t</thead>\r\n");
		sb.append("\t<tbody>\r\n");
		sb.append("\t\t<%\r\n");
		sb.append("\t\tfor(int i =0;i<"+getLowcaseName(pojo)+"List.size();i++){\r\n");
		sb.append("\t\t\t"+pojo.getName()+" tmp = "+getLowcaseName(pojo)+"List.get(i);\r\n");
		sb.append("\t\t%>\r\n");
		sb.append("\t\t<tr class=\"<%=i%2==0?\"tr2\":\"tr1\"%>\">\r\n" );
		
		for(int i = 0;i<attrList.size();i++){
			Attr attr = attrList.get(i);
			String classstr = "td2";
			if(i%2==0){
				classstr = "td1";
			}
			sb.append("\t\t\t<td class=\""+classstr+"\" align=\"center\"><%=tmp.get"+getUpcaseName(attr.getName())+"()%></td>\r\n");
		}
		
		sb.append("\t\t</tr>\r\n");
		sb.append("\t\t<%}%>\r\n");
		sb.append("\t</tbody>\r\n");
		sb.append("</table>\r\n");
		return sb.toString();
	}
	
	public String toAddForm(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("<form method=\"post\" action=\""+getLowcaseName(pojo)+"_do.jsp\">\r\n");
		sb.append("<input type=\"hidden\" name=\"m\" value=\"add\"/>\r\n");
		sb.append("<table class=\""+formtableClass+"\" style=\"width:100%;\">\r\n");
		for(Attr attr : attrList){
			sb.append("\t<tr style=\"height:5px\"></tr>\r\n");
			sb.append("\t<tr>\r\n");
			sb.append("\t\t<td>"+attr.getName()+"</td>\r\n");
			sb.append("\t\t<td><input type=\"text\" name=\""+getLowcaseName(attr.getName())+"\" id=\""+getLowcaseName(attr.getName())+"\" value=\"\"/></td>\r\n");
			sb.append("\t</tr>\r\n");
		}
		sb.append("</table>\r\n");
		sb.append("</form>\r\n");
		return sb.toString();
	}
	
	public String toUpdateForm(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		sb.append("<form method=\"post\" action=\""+getLowcaseName(pojo)+"_do.jsp\">\r\n");
		sb.append("<input type=\"hidden\" name=\"m\" value=\"update\"/>\r\n");
		sb.append("<table class=\""+formtableClass+"\" style=\"width:100%;\">\r\n");
		for(Attr attr : attrList){
			sb.append("\t<tr style=\"height:5px\"></tr>\r\n");
			sb.append("\t<tr>\r\n");
			sb.append("\t\t<td>"+attr.getName()+"</td>\r\n");
			sb.append("\t\t<td><input type=\"text\" name=\""+getLowcaseName(attr.getName())+"\" id=\""+getLowcaseName(attr.getName())+"\" value=\"<%=tmp.get"+getUpcaseName(attr.getName())+"()%>\"/></td>\r\n");
			sb.append("\t</tr>\r\n");
		}
		sb.append("</table>\r\n");
		sb.append("</form>\r\n");
		return sb.toString();
	}
	
	public String toJsp(Pojo pojo){
		StringBuffer sb = new StringBuffer();
		List<Attr> attrList = pojo.getAttrList();
		for(int i =0;i<attrList.size();i++){
			Attr attr = attrList.get(i);
			if(attr.getType()==1){
				sb.append("String "+getLowcaseName(attr.getName())+" = RequestUtil.toStr(request,\""+getLowcaseName(attr.getName())+"\",\"\");\r\n");
			}else if(attr.getType()==2){
				sb.append("int "+getLowcaseName(attr.getName())+" = RequestUtil.toInt(request,\""+getLowcaseName(attr.getName())+"\",0);\r\n");
			}else if(attr.getType()==3){
				sb.append("Date "+getLowcaseName(attr.getName())+" = RequestUtil.toDate(request,\""+getLowcaseName(attr.getName())+"\",\"yyyy-MM-dd HH:mm\",new Date());\r\n");
			}else{
				sb.append("*类型* "+getLowcaseName(attr.getName())+" = RequestUtil.toStr(request,\""+getLowcaseName(attr.getName())+"\",\"\");\r\n");
			}
		}
		sb.append("\r\n");
		sb.append(pojo.getName()+" "+getLowcaseName(pojo)+" = new "+pojo.getName()+"();\r\n");
		sb.append("\r\n");
		for(int i =0;i<attrList.size();i++){
			Attr attr = attrList.get(i);
			sb.append(getLowcaseName(pojo)+".set"+getUpcaseName(attr.getName())+"("+getLowcaseName(attr.getName())+");\r\n");
		}
		return sb.toString();
	}
	
	public String getLowcaseName(String str){
		return str.substring(0,1).toLowerCase() + str.substring(1);
	}
	
	public String getLowcaseName(Pojo pojo){
		return getLowcaseName(pojo.getName());
	}
	public String getUpcaseName(String str){
		return str.substring(0,1).toUpperCase() + str.substring(1);
	}
	
	public String getUpcaseName(Pojo pojo){
		return getUpcaseName(pojo.getName());
	}
	
	
	public static void main(String[] args) {
		ToHtml ts = new ToHtml();
		Pojo p = new Pojo();
		p.setName("Doc");
		Attr attr1=  new Attr();
		attr1.setName("id");
		attr1.setType(2);
		Attr attr2=  new Attr();
		attr2.setName("docTitle");
		attr2.setType(1);
		Attr attr3=  new Attr();
		attr3.setName("subtitle");
		attr3.setType(1);
		Attr attr4=  new Attr();
		attr4.setName("htmlcontent");
		attr4.setType(1);
		Attr attr5=  new Attr();
		attr5.setName("crtime");
		attr5.setType(3);
		Attr attr6=  new Attr();
		attr6.setName("doctime");
		attr6.setType(3);
		List l = new ArrayList<Attr>();
		l.add(attr1);
		l.add(attr2);
		l.add(attr3);
		l.add(attr4);
		l.add(attr5);
		l.add(attr6);
		p.setAttrList(l);
		System.out.println(ts.toJsp(p));
	}
}
